//
//  ViewController.m
//  lianxi
//
//  Created by mac on 16-4-26.
//  Copyright (c) 2016年 mac. All rights reserved.
//
#import "ViewController.h"
@interface PhoneData : NSObject
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *tel;
@end
@implementation PhoneData
@end
@interface ViewController ()
{
    
    NSArray *data;
    
    
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.dataSource = self;
    [self.view insertSubview:tableView aboveSubview:tableView];
    NSBundle *bundle = [NSBundle mainBundle];
    NSURL * url =[bundle URLForResource:@"asd" withExtension:@"vcf"];
    NSString * text = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSArray * lines = [text componentsSeparatedByString:@"\n"];
    PhoneData *item;
    NSMutableArray *d = [NSMutableArray new];
    NSMutableSet <NSString *>*set=[NSMutableSet set];
    for (NSString *line in lines) {
        if ([line hasPrefix:@"BEGIN"]) {
            item = [PhoneData new];
        }else if ([line hasPrefix:@"END"]){
            
            
            [d addObject:item];
        }else if ([line hasPrefix:@"N;"]){
            NSArray *upperComponents = [line componentsSeparatedByString:@":"];
            NSArray *components =[[upperComponents objectAtIndex:1]componentsSeparatedByString:@";"];
            NSString *lastName = [components objectAtIndex:0];
            NSString *firstName = [components objectAtIndex:1];
            lastName = [lastName stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
            firstName = [firstName stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
            
        }else if ([line hasPrefix:@"EMAIL;"])
        {
            NSArray *components = [line componentsSeparatedByString:@":"];
            NSString *emailAddress = [components objectAtIndex:1];
            // NSlog(@"emailAddress %@",emailAddress);
            
        }else if ([line hasPrefix:@"TEL;"])
        {
            NSArray *components = [line componentsSeparatedByString:@":"];
            NSString *phoneNumber = [components objectAtIndex:1];
            
            item.tel= [phoneNumber substringToIndex:11];
            //substring 子字符串 to 来 index 指数
        }else if ([line hasPrefix:@"FN;"]){
            //FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E4=BD=99=E9=86=B4
            NSArray *upperComponents = [line componentsSeparatedByString:@":"];
            NSString *name = upperComponents[1];
            name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
            name = [name stringByRemovingPercentEncoding];
            // removing 删除 percent 百分比 encoding 编码
            item.name = name;
        }else if ([line hasPrefix:@"ORG;"]){
            NSArray *upperComponents = [line componentsSeparatedByString:@":"];
            NSString *name = upperComponents[1];
            name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
            name = [name stringByRemovingPercentEncoding];
            // NSlog(@"ORG:%@",name);
            item.tel= name ? : @"未知";
            [set addObject:item.tel];
        }
    }
    data = d;
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  data.count;
    
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    PhoneData *d=data[indexPath.row];
    cell.textLabel.text= d.name;
    cell.detailTextLabel.text= d.tel;
    return cell;
    
    
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
    
    
    
    
    
}





-(void)didChangeValueForKey:(NSString *)key {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
